package com.freedom.mediaplayer;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.Toast;

import com.freedom.mediaplayer.app.AppConstant;
import com.freedom.mediaplayer.fragment.HomeFragment;
import com.freedom.mediaplayer.service.PlayService;
import com.freedom.mediaplayer.until.DataUtil;
import com.freedom.mediaplayer.until.MusicUntil;
import com.freedom.mediaplayer.until.SharedPrefencetUntil;

public class MainActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.content);
//		Fragment fragment1 = new Fragment();
//		getSupportFragmentManager().beginTransaction()
//				.replace(R.id.content_frame, fragment1).commit();
		HomeFragment homeFragment = new HomeFragment();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, homeFragment).commit();
		DataUtil.curPosition = SharedPrefencetUntil.getSharedPrefence(this);
		DataUtil.list = MusicUntil.getMp3Infos(this);
		// 添加来电监听事件
		TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 获取系统服务
		telManager.listen(new MobliePhoneStateListener(),
				PhoneStateListener.LISTEN_CALL_STATE);
		System.out.println(DataUtil.list);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		DataUtil.curPosition = SharedPrefencetUntil.getSharedPrefence(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//System.out.println("This Stop"+DataUtil.curPosition);
		SharedPrefencetUntil.setSharedPrefence(this, DataUtil.curPosition);	
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		SharedPrefencetUntil.setSharedPrefence(this, DataUtil.curPosition);	
		Intent intent = new Intent(MainActivity.this, PlayService.class);
		stopService(intent); 
		DataUtil.isPlaying = false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			handleCilick();// 点击就跳转到此方法
			return true;

		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	private boolean isDoubleClick;
	Timer timer = new Timer();

	private void handleCilick() {
		// TODO Auto-generated method stub
		//System.out.println(isDoubleClick);
		if (!isDoubleClick) {
			isDoubleClick = true;
			// 第二次点击，用到了TimerTask
			Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					isDoubleClick = false;
				}
			}, 2000);
		} else {
			exit();
		}

	}

	/**
	 * 退出程序
	 */
	private void exit() {
		Intent intent = new Intent(MainActivity.this, PlayService.class);
		stopService(intent);
		finish();
	}

	/**
	 * @author Yang
	 *  电话监听器类
	 */
	private class MobliePhoneStateListener extends PhoneStateListener {
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE: // 挂机状态
				Intent intent = new Intent(MainActivity.this, PlayService.class);
				intent.setAction("com.example.media.MUSIC_SERVICE");
				intent.putExtra("MSG", AppConstant.PlayerMsg.CONTINUE_MSG); // 继续播放音乐
				startService(intent);
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK: // 通话状态
			case TelephonyManager.CALL_STATE_RINGING: // 响铃状态
				Intent intent2 = new Intent(MainActivity.this,
						PlayService.class);
				intent2.setAction("com.example.media.MUSIC_SERVICE");
				intent2.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG);
				startService(intent2);
				break;
			}
		}
	}
}
